package com.whp.yky.yky.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whp.yky.yky.entity.poentity.Datas;

import com.whp.yky.yky.response.ResponseData;
import com.whp.yky.yky.service.serviceimpl.DataServiceImpl;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Api(tags = "资料接口")
@CrossOrigin
@RestController
public class DataController {

    @Autowired
    private DataServiceImpl dataServiceImpl;



    @ApiOperation(value="上传资料", notes="上传资料")
    @PostMapping("/insertData")
    public ResponseData insertDataInfo(@RequestBody  Datas datas ){



        ResponseData res = new ResponseData();
        boolean result= dataServiceImpl.insertDataInfo(datas);
        if (result){
            res.setCode(20000);
            res.setData("success");



        }else{
            res.setCode(50000);
            res.setData("fail");
        }



        return res;
    }

    @DeleteMapping("/deletDataByID/{id}")
    @ApiOperation(value="删除资料", notes="根据id删除资料")
    public ResponseData deletDataByID(@PathVariable("id") int id){
        boolean result = dataServiceImpl.deleteDataInfo(id);
        ResponseData res = new ResponseData();

        if (result){
            res.setCode(20000);
            res.setData("success");

        }else{
            res.setCode(50000);
            res.setData("fail");
        }
        return res;
    }

    @ApiOperation(value="修改资料信息", notes="修改资料信息")
    @PostMapping("/updateDataInfo")
    public ResponseData updateDataInfo(@RequestBody Datas datas,@RequestParam String username){

        boolean result = false;
        ResponseData res = new ResponseData();

        if (result){
            res.setCode(20000);
            res.setData("success");

        }else{
            res.setCode(50000);
            res.setData("fail");
        }
        return res;
    }

    @ApiOperation(value="获取所有资料信息", notes="获取所有资料信息")
    @PostMapping("/getDataInfo")
    public ResponseData getDataInfo(@RequestBody Datas datas,@RequestParam Integer pageNum, @RequestParam Integer pageSize){

        ResponseData res = new ResponseData();


        res.setCode(20000);
        IPage<Datas> pageList = this.dataServiceImpl.getDataInfo(new Page((long)pageNum, (long)pageSize),datas);
        res.setData(pageList);
//        res.setTotal(dataServiceImpl.getDataInfo(datas).size());


        return res;
    }



    @ApiOperation(value="根据id获取档案信息", notes="根据id获取档案信息")
    @GetMapping("/getDataInfoById")
    public ResponseData getDatatInfoById(@RequestParam Integer id){
        ResponseData res = new ResponseData();
        res.setCode(20000);
        Datas datas = dataServiceImpl.getDataInfoById(id);
        res.setData(datas);
//        res.setTotal(documentServiceImpl.getDocumentInfo(document).size());

        return res;

    }

}
